package com.qyhl.module_home.main;


import com.orhanobut.hawk.Hawk;
import com.qyhl.module_home.common.HomeUrl;
import com.qyhl.webtv.basiclib.utils.network.EasyHttp;
import com.qyhl.webtv.basiclib.utils.network.callback.SimpleCallBack;
import com.qyhl.webtv.basiclib.utils.network.exception.ApiException;
import com.qyhl.webtv.commonlib.common.CommonUtils;
import com.qyhl.webtv.commonlib.entity.config.AppConfigBean;
import com.qyhl.webtv.commonlib.entity.home.TokenBean;

/**
 * Created by PVer on 2017/10/3.
 * 启动页模型
 */

public class MainModel implements MainContract.MainModel {
    private MainPresenter mPresenter;

    MainModel(MainPresenter mainPresenter) {
        this.mPresenter = mainPresenter;
    }

    /**
     * 获取配置信息
     */
    @Override
    public void getConfig() {

        String tag = CommonUtils.getInstance().getSiteName();
        String site = CommonUtils.getInstance().getSite() + "";

        EasyHttp.get(HomeUrl.GET_APP_CONFIG_NEW)
                .params("tagName", tag)
                .params("siteId", site)
                .execute(new SimpleCallBack<AppConfigBean>() {
                    @Override
                    public void onError(ApiException e) {
                        if (e.getCode() == 202) {
                            mPresenter.setConfig(null);
                        } else {
                            mPresenter.configHttpError();
                        }

                    }

                    @Override
                    public void onSuccess(AppConfigBean appConfigBean) {
                        Hawk.put("config", appConfigBean);
                        mPresenter.setConfig(appConfigBean);
                    }
                });

    }

    /**
     * 判断token是否失效
     *
     * @param token
     */
    @Override
    public void compareToken(String token) {
        EasyHttp.post(HomeUrl.GET_COMPARE_TOKEN)
                .params("token", token)
                .execute(new SimpleCallBack<TokenBean>() {
                    @Override
                    public void onError(ApiException e) {
                        mPresenter.compareError();
                    }

                    @Override
                    public void onSuccess(TokenBean tokenBean) {
                        mPresenter.compareSuccess(tokenBean);
                    }
                });

    }


}
